package com.hejiu45.hejiu45_api_test.controller;

import com.hejiu45.hejiu45_api_test.controller.utils.R;
import com.hejiu45.hejiu45_api_test.entity.Users;
import com.hejiu45.hejiu45_api_test.mapper.UsersMapper;
import com.hejiu45.hejiu45_api_test.service.IUsersService;
import com.hejiu45.hejiu45_api_test.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
//@CrossOrigin
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private IUsersService iUsersService;

    @Autowired
    private UsersMapper usersMapper;

    @GetMapping
    public R SelectAll() {
        return new R(true, iUsersService.list());
    }

    @PostMapping
    public R Save(@RequestBody Users users) {
        return new R(iUsersService.save(users));
    }

    @PutMapping
    public R Update(@RequestBody Users users) {
        return new R(iUsersService.modify(users));
    }

    @DeleteMapping("{id}")
    public R Delete(@PathVariable Integer id) {
        return new R(iUsersService.removeById(id));
    }

    @GetMapping("{id}")
    public R SelectById(@PathVariable Integer id) {
        return new R(true, iUsersService.getById(id));
    }

    @GetMapping("{currentPage}/{pageSize}")
    public R SelectPage(@PathVariable int currentPage, @PathVariable int pageSize) {
        return new R(true, iUsersService.getPage(currentPage, pageSize));
    }

    //    搜索
    @GetMapping("searchs/{userslike}")
    public R getUsersLike(@PathVariable String userslike) {
        return new R(true, iUsersService.getUsersLike(userslike));
    }

//    登录认证的方法
    @PostMapping("login")
    public Map<String, Object> login(@RequestBody Users users) {
        log.info("用户名称：[{}]", users.getUname());
        log.info("用户密码：[{}]", users.getUpasswd());
        Map<String, Object> map = new HashMap<>();

        try {
            Users userDB = iUsersService.login(users);


            if (userDB == null) {
                map.put("state", false);
                map.put("msg", "当前账户【" + users.getUname() + "】不存在！" );
                return map;
            } else {
                //            给token payload传参
                HashMap<String, String> payload = new HashMap<>();
                payload.put("uid", String.valueOf(userDB.getId()));
                payload.put("uname", userDB.getUname());
//            生成JWT的令牌
                String token = JWTUtils.getToken(payload);
                map.put("state", true);
                map.put("msg", "认证成功");
                map.put("token", token);

                return map;
            }

        } catch (Exception e) {
            map.put("state", false);
            map.put("msg", "认证失败" + e.getMessage());

            return map;
        }

    }

    @GetMapping("logout")
    public R logout(){
        return new R(true, "账号退出成功！");
    }

}
